//
//  DeviceInfo.swift
//  ttools
//
//  Created by tangjilin on 2025/1/28.
//

import UIKit
import CoreGraphics
import Network
import Foundation

/// 获取设备的宽高 IOS 平台
/// - Returns: description
@MainActor public func getDeviceScreenSize() -> (width:CGFloat, height:CGFloat) {
    
    let screenSize = UIScreen.main.bounds.size
    
    return (width:screenSize.width,height:screenSize.height)
}

/// 获取设备唯一标识符
/// - Returns: description
@MainActor public func getIdentifierForVendor() -> String? {
    return UIDevice.current.identifierForVendor?.uuidString
}

/// 获取设备型号
/// - Returns: description
@MainActor public func getDeviceModel() -> String {
    let device = UIDevice.current
    return "Device Model: \(device.model), System Name: \(device.systemName), System Version: \(device.systemVersion)"
}

/// 获取设备方向
/// - Returns: description
@MainActor public func getDeviceOrientation() -> String {
    let orientation = UIDevice.current.orientation
    switch orientation {
    case .portrait: return "Portrait"
    case .landscapeLeft: return "Landscape Left"
    case .landscapeRight: return "Landscape Right"
    case .portraitUpsideDown: return "Portrait Upside Down"
    default: return "Unknown"
    }
}

@MainActor
public func checkNetworkType(completion: @escaping @Sendable (String) -> Void) {
    let monitor = NWPathMonitor()
    
    monitor.pathUpdateHandler = { path in
        if path.status == .satisfied {
            if path.usesInterfaceType(.wifi) {
                completion("Connected via WiFi")
            } else if path.usesInterfaceType(.cellular) {
                completion("Connected via Cellular")
            } else {
                completion("Connected via Other Interface")
            }
        } else {
            completion("No connection")
        }
        // 停止监测
        monitor.cancel()
    }
    
    let queue = DispatchQueue(label: "NetworkMonitor")
    monitor.start(queue: queue)
}


/// 获取可用内存和存储空间
/// - Returns: description
@MainActor
public func getMemoryInfo() -> (physicalMemory: UInt64, freeMemory: UInt64) {
    let physicalMemory = ProcessInfo.processInfo.physicalMemory
    var freeMemory: UInt64 = 0
    
    freeMemory = 1000000000 // 例如：1GB（这里只是示意值）

    return (physicalMemory, freeMemory)
}



//MARK: 输出封装
public func TLog<T>(_ message: T, file: String = #file, function: String = #function, line: Int = #line) {
    #if DEBUG
    // 获取文件名
    let fileName = (file as NSString).lastPathComponent
    // 打印日志内容
    print("\(fileName):\(line) \(function) | \(message)")
    #endif
}

